<template>
	<view>
		<u-loading-page :loading="pageLoading"></u-loading-page>
		<u-sticky>
			<view class="bgc-white p-20">
				<u-search placeholder="按条码搜索" v-model="searchData.keywords" @search="onSearch"></u-search>
			</view>
		</u-sticky>
		
		<u-modal :show="modalShow" @confirm="onDeleteConfirm" @cancel="modalShow = false" ref="uModal" showCancelButton
			:asyncClose="true" title="确定要删除该记录吗？">
		</u-modal>


		<scroll-view class="" scroll-y refresher-enabled show-scrollbar
			:refresher-triggered="triggered" @refresherrefresh="$u.throttle(onRefresherrefresh, 200)"
			@scrolltolower='$u.throttle(onScrolltolower,1000)' :style="{height: `${screenHeight}px`}">
			
			<u-empty mode="list" v-if="listData.length === 0" :style="{height: `${screenHeight}px`}" />
			
			<!-- 列表 -->
			<view class="p-l-30 p-r-30">
				<view class="item" v-for="(item, index) in listData" :key="index">
					<view class="delete-btn">
						<u-button type="error" shape="circle" size="small" @tap="btnDelete(item)">删除</u-button>
					</view>
					
					<view class="flex-sb f-40">
						<view>
							{{item.code}} / {{item.name}}
						</view>
						<view v-if="item.isCancel">
							<u-tag type='info' text="已作废" />
						</view>
					</view>
					<view class="flex-sb c-99 m-t-10">
						<view class="f-30">颜色：{{item.colorNumber}} , {{item.color}}</view>
						<view class="f-30">规格：{{item.spec}}</view>
						<view class="f-30">缸号：{{item.vatNo}}</view>
					</view>

					<view class="flex-sb m-t-20">
						<view class="f-30">仓库：{{item.storeName}}</view>
						<view class="f-30">数量：{{item.quantity}}</view>
					</view>
					<view class="flex-sb m-t-20">
						<view class="f-30">条码：{{item.sn}}</view>
					</view>
					<view class="flex-sb m-t-10" v-if="item.billId">
						<view class="c-99">{{item.billNo}}</view>
						<view class="business-time">{{item.billBusinessTime}}</view>
					</view>
					<view class="remarks">{{item.remarks}}</view>
				</view>
			</view>

		</scroll-view>
	</view>
</template>

<script>
	import {
		listPage,
		deleteById
	} from '@/apis/bill/scan-sn.js'

	export default {
		data() {
			return {
				modalShow:false,
				currentRow: null,
				searchData: {},
				screenHeight: 0,
				pageLoading: true,
				triggered: false,
				listData: [],
				pageInfo: {
					orders: [{
						column: "create_time",
						asc: false,
					}],
					current: 1, // 当前页
					size: 10, // 每页数据条数
					sort: '', // 排序字段
					order: 'desc', // 排序方向
					total: 0 // 总数居量
				}
			}
		},
		onLoad() {
			const params = this.$Route.query
			const detailId = params.detailId
			this.$set(this.searchData, "detailId", detailId)
			this.initData()
		},
		onReady() {
			const sysInfo = uni.getSystemInfoSync()
			this.screenHeight = sysInfo.windowHeight
		},
		methods: {
			btnDelete(row) {
				this.modalShow = true
				this.currentRow = row
			},
			onDeleteConfirm() {
				const currentRow = this.currentRow
				const scanId = currentRow.id
				deleteById(scanId).then(res => {
					if (!res.data.flag) return
					const data = res.data.data
					this.listData = []
					this.listPage()
					uni.showToast({
						title: res.data.message
					})
				}).finally(_ => {
					this.modalShow = false
				})
			},
			initData() {
				this.listPage()
			},
			listPage() {
				const params = {
					page: this.pageInfo,
					data: this.searchData
				}
				listPage(params).then(res => {
					if (!res.data.flag) return
					const records = res.data.data.records || []
					this.listData.push(...records)
				}).finally(_ => {
					this.triggered = false
					this.pageLoading = false
				})
			},
			onSearch() {
				this.listData = []
				this.listPage()
			},
			itemClick(row) {
				this.$Router.push({
					path: "/label/detail",
					query: {
						oper: 'edit',
						id: row.id
					}
				})
			},
			onRefresherrefresh() {
				this.triggered = true
				this.listData = []
				this.listPage()
			},
			onScrolltolower() {}
		}
	}
</script>

<style lang="scss" scoped>
	.item {
		border-radius: 10rpx;
		background: #fff;
		padding: 30rpx;
		margin-top: 30rpx;
		position: relative;
		
		.delete-btn {
			top: 14rpx;
			right: 20rpx;
			position: absolute;
		}

		.comego {
			font-size: 40rpx;
		}

		.bill-no {
			font-size: 30rpx;
			color: $uni-text-color;
		}

		.business-time {
			color: $uni-text-color-grey;
		}

		.remarks {
			font-size: 24rpx;
			margin-top: 20rpx;
			color: $uni-text-color-placeholder;
		}
	}
</style>